package com.sunxueqi.framework.security.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import com.alibaba.fastjson.JSON;
import com.sunxueqi.common.utils.ServletUtils;
import com.sunxueqi.framework.security.LoginUser;
import com.sunxueqi.framework.security.service.TokenService;
import com.sunxueqi.framework.web.vo.ResponseData;

import cn.hutool.core.util.ObjectUtil;

@Configuration
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler{

	@Autowired
    private TokenService tokenService;
	
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		LoginUser loginUser = tokenService.getLoginUser(request);
		if (ObjectUtil.isNotNull(loginUser)) {
			tokenService.deleteLoginUser(loginUser.getToken());
			//记录退出日志
		}
		ServletUtils.renderString(response, JSON.toJSONString(ResponseData.success("退出成功")));
	}

}
